package init

import (
	"day2zy/config"
	"day2zy/model"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func Mysql() {
	var err error
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	mysqlAppConf := config.AppConf.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlAppConf.User,
		mysqlAppConf.Password,
		mysqlAppConf.Host,
		mysqlAppConf.Port,
		mysqlAppConf.Database)
	fmt.Println(dsn)
	config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库连接成功")
	err = config.DB.AutoMigrate(&model.Scene{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据迁移成功")
}
